var express = require('express');
const bookstore = express.Router();
var bodyParser = require('body-parser');
const path = require("path");
const Database = require("better-sqlite3");
//获取数据库中管理员账号密码等信息
const mydb = new Database(path.resolve(__dirname, "../database/infor.db"), { verbose: console.log });

// 创建 application/x-www-form-urlencoded 编码解析
bookstore.use(bodyParser.urlencoded({ extended: false }))
bookstore.use(express.json());
bookstore.use(bodyParser.json());

bookstore.post('/bookstore', function (req, res) {
    // 获取前端数据
    var response = {
        "ISBN": req.body.ISBN,
        "code": req.body.code,
        "bookname": req.body.bookname,
        "author": req.body.author,
        "translator": req.body.translator,
        "publisher": req.body.publisher,
        "bookcase": req.body.bookcase,
        "booktype": req.body.booktype,
        "price": req.body.price,
        "introduce": req.body.introduce,
        "total_inventory": req.body.total_inventory,
        "publish_num": req.body.publish_num,
        "operator": req.body.operator,
        "buy_data": req.body.date
    };
    // console.log(response);
    //指定数据库文件路径
    const get_user_by_id = mydb.prepare('SELECT * FROM book WHERE code= ?');//一个查询语句对象
    var a = get_user_by_id.get(response.code);//执行查询
    // console.log(a);
    if (a) {
        // 如果查询到了说明已经有这本书了
        // 确定ISBN是否相同
        // console.log(a.ISBN);
        if (a.ISBN == response.ISBN) {
            console.log("已有该图书");
            // 如果相同则只需要加库存
            const updata_stmt = mydb.prepare('UPDATE book SET total_inventory =total_inventory+@total_inventory,logoff=@logoff  WHERE ISBN=@ISBN')
            updata_stmt.run({ ISBN: response.ISBN, logoff: "no", total_inventory: response.total_inventory });
            res.sendFile(path.resolve(__dirname, "../public/html/store_success.html"));
        }
        else {
            // 如果ISBN不同的话，说明输入有问题
            res.sendFile(path.resolve(__dirname, "../public/html/store_error.html"));
        }
    }
    else {
        // 如果没有查询到，说明这是第一批购入，在数据库中插入此书数据
        // 如果不相同，就要插入新的图书信息
        const insert = mydb.prepare('INSERT INTO book (ISBN,code,bookname,author,translator,publisher,bookcase,booktype,price,introduce,inventory,total_inventory,publish_num,operator,buy_time,logoff) VALUES (@ISBN,@code,@bookname,@author,@translator,@publisher,@bookcase,@booktype,@price,@introduce,@inventory,@total_inventory,@publish_num,@operator,@buy_data,@logoff)');
        //插入一条记录
        insert.run({ ISBN: response.ISBN, code: response.code, bookname: response.bookname, author: response.author, translator: response.translator, publisher: response.publisher, bookcase: response.bookcase, booktype: response.booktype, price: response.price, introduce: response.introduce, inventory: response.total_inventory, total_inventory: response.total_inventory, publish_num: response.publish_num, operator: response.operator, buy_data: response.buy_data, logoff: "no" });
        res.sendFile(path.resolve(__dirname, "../public/html/store_success.html"));
    }
});

module.exports = bookstore;